package com.glq1218.category.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.glq1218.category.bo.AddCategoryBO;
import com.glq1218.category.bo.CategoryBO;
import com.glq1218.category.bo.QueryCategoryBO;
import com.glq1218.category.bo.UpdateCategoryBO;
import com.glq1218.category.dto.AddCategoryDTO;
import com.glq1218.category.dto.CategoryDTO;
import com.glq1218.category.dto.QueryCategoryDTO;
import com.glq1218.category.dto.UpdateCategoryDTO;
import com.glq1218.category.entity.Category;
import com.glq1218.category.service.CategoryService;
import com.glq1218.domain.*;
import com.glq1218.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author glq1218
 * @description 物品类别控制器
 * @since 2023/4/21 14:55
 */
@RestController
@RequestMapping("/api/category")
@RequiredArgsConstructor
public class CategoryController {
    private final CategoryService categoryService;

    @PostMapping
    public R<?> addCategory(@RequestBody AddCategoryDTO addCategoryDTO) {
        AddCategoryBO addCategoryBO = BeanCopyUtils.copyBean(addCategoryDTO, AddCategoryBO.class);
        return R.ok(categoryService.addCategory(addCategoryBO));
    }

    @GetMapping
    public R<PaginationResult<CategoryDTO>> queryCategoryList(PaginationParam paginationParam, SortParam sortParam, IntervalStr intervalStr, QueryCategoryDTO queryCategoryDTO) {
        QueryCategoryBO queryCategoryBO = BeanCopyUtils.copyBean(queryCategoryDTO, QueryCategoryBO.class);
        PaginationResult<CategoryBO> paginationResult = categoryService.queryCategoryList(paginationParam, sortParam, intervalStr, queryCategoryBO);
        // 获取BO转换为DTO
        List<CategoryDTO> categoryDTOList = new ArrayList<>();
        // 结果不为空才能进行转换
        if (Objects.nonNull(paginationResult.getRecords())) {
            List<CategoryBO> records = paginationResult.getRecords();
            categoryDTOList = BeanCopyUtils.copyBeanList(records, CategoryDTO.class);
        }
        // 返回封装好的分页查询DTO
        return R.ok(PaginationResult.<CategoryDTO>builder()
                .total(paginationResult.getTotal())
                .records(categoryDTOList)
                .build()
        );
    }

    @GetMapping("/{id}")
    public R<CategoryDTO> getCategoryById(@PathVariable("id") Long id) {
        Category category = categoryService.getById(id);
        CategoryDTO categoryDTO = BeanCopyUtils.copyBean(category, CategoryDTO.class);
        return R.ok(categoryDTO);
    }

    @GetMapping("/queryAllCategory")
    public R<List<CategoryDTO>> queryAllCategory() {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getStatus, 0);
        List<Category> categories = categoryService.getBaseMapper().selectList(lambdaQueryWrapper);
        List<CategoryDTO> categoryDTOList = BeanCopyUtils.copyBeanList(categories, CategoryDTO.class);
        return R.ok(categoryDTOList);
    }

    @PutMapping
    public R<?> updateCategory(@RequestBody UpdateCategoryDTO updateCategoryDTO) {
        UpdateCategoryBO updateCategoryBO = BeanCopyUtils.copyBean(updateCategoryDTO, UpdateCategoryBO.class);
        return R.ok(categoryService.updateCategory(updateCategoryBO));
    }

    @DeleteMapping
    public R<?> deleteCategory(@RequestParam("idList") List<Long> idList) {
        return R.ok(categoryService.removeByIds(idList));
    }
}
